package coc.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import coc.core.collector.CollectorType;
import coc.core.filter.BloomFilter;
import coc.core.url.Url;

import com.gargoylesoftware.htmlunit.BrowserVersion;

/**
 * 爬虫配置。
 */
public class CrawlerConfig implements Serializable {
	/** 任务ID */
	private String taskId;
	/** 任务名称 */
	private String taskName;
	/** 起始URL */
	private Url startUrl;
	/** 并发作业数 */
	private Integer threadCount = 20;
	/** 爬虫作业超时时间 */
	private Integer jobTimeout = 60;
	/** 超时时间 */
	private Integer timeout = 5;
	/** 采集最大深度 */
	private Integer maxDepth = 2;
	/** 轮询间隔时间（毫秒） */
	private Integer interval = 1000;
	/** 采集器采集页面等待时间（毫秒），仅对HTMLUNIT采集器生效 */
	private Integer waitForGetPage = 1000;
	/** 爬虫脚本 */
	private String script;
	/** 采集器类型 */
	private CollectorType collectorType = CollectorType.JSOUP;
	/** 浏览器类型 */
	private BrowserVersion browserVersion = BrowserVersion.getDefault();
	/** 采集器条件设置 */
	private Map<String, String> collectorTerms = new LinkedHashMap<String, String>();
	/** Cookie */
	private List<CrawlerCookie> cookies = new ArrayList<CrawlerCookie>();
	/** 待处理的URL列表 */
	private LinkedBlockingQueue<Url> urls = new LinkedBlockingQueue<Url>();
	/** 布隆过滤器 */
	private BloomFilter bloomFilter;

	/**
	 * 获取下一批待处理链接。
	 * 
	 * @return 返回下一批待处理链接。
	 */
	public List<Url> getSuspendUrls() {
		List<Url> suspendUrls = new ArrayList<Url>();
		urls.drainTo(suspendUrls, threadCount);
		return suspendUrls;
	}

	/**
	 * 增加待处理链接，增加的同时会在本地做去重处理。
	 * 
	 * @param url
	 *            待处理链接
	 */
	public void addSuspendUrl(Url url) {
		if (!bloomFilter.contains(url.toString())) {
			urls.add(url);
			bloomFilter.add(url.toString());
		}
	}

	/**
	 * 增加采集器构建条件。
	 * 
	 * @param term
	 *            构建条件
	 * @param collectorType
	 *            采集器类型
	 */
	public void addCollectorTerm(String term, String collectorType) {
		collectorTerms.put(term, collectorType);
	}

	/**
	 * 判断是否还有待处理的链接。
	 * 
	 * @return 如果还有待处理的链接返回true，否则返回false。
	 */
	public Boolean hasSuspendUrls() {
		return !urls.isEmpty();
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public Url getStartUrl() {
		return startUrl;
	}

	public void setStartUrl(Url url) {
		this.startUrl = url;
	}

	public Integer getThreadCount() {
		return threadCount;
	}

	public void setThreadCount(Integer threadCount) {
		this.threadCount = threadCount;
	}

	public Integer getJobTimeout() {
		return jobTimeout;
	}

	public void setJobTimeout(Integer jobTimeout) {
		this.jobTimeout = jobTimeout;
	}

	public Integer getTimeout() {
		return timeout;
	}

	public void setTimeout(Integer timeout) {
		this.timeout = timeout;
	}

	public Integer getMaxDepth() {
		return maxDepth;
	}

	public void setMaxDepth(Integer maxDepth) {
		this.maxDepth = maxDepth;
	}

	public Integer getInterval() {
		return interval;
	}

	public void setInterval(Integer interval) {
		this.interval = interval;
	}

	public Integer getWaitForGetPage() {
		return waitForGetPage;
	}

	public void setWaitForGetPage(Integer waitForGetPage) {
		this.waitForGetPage = waitForGetPage;
	}

	public String getScript() {
		return script;
	}

	public void setScript(String script) {
		this.script = script;
	}

	public CollectorType getCollectorType() {
		return collectorType;
	}

	public void setCollectorType(CollectorType collectorType) {
		this.collectorType = collectorType;
	}

	public BrowserVersion getBrowserVersion() {
		return browserVersion;
	}

	public void setBrowserVersion(BrowserVersion browserVersion) {
		this.browserVersion = browserVersion;
	}

	public Map<String, String> getCollectorTerms() {
		return collectorTerms;
	}

	public List<CrawlerCookie> getCookies() {
		return cookies;
	}

	public void setCookies(List<CrawlerCookie> cookies) {
		this.cookies = cookies;
	}

	public LinkedBlockingQueue<Url> getUrls() {
		return urls;
	}

	public void setUrls(LinkedBlockingQueue<Url> urls) {
		this.urls = urls;
	}

	public BloomFilter getBloomFilter() {
		return bloomFilter;
	}

	public void setBloomFilter(BloomFilter bloomFilter) {
		this.bloomFilter = bloomFilter;
	}

	public void setCollectorTerms(Map<String, String> collectorTerms) {
		this.collectorTerms = collectorTerms;
	}
}
